package rsee.com;

import java.util.LinkedList;

import android.os.Handler;
import ezeye.util.EyeBuffer;

public class EyeSocketWrap {
    private SourceIdent mIdent = null;
    private Handler mTransHandler = null;
    private EyeSocket mSocket = null;
    private EyeBuffer mRawBuffer = null;
    private byte[] mSocketBuffer = null;
    private LinkedList<byte[]> mRawDataStack = null;
    private int Version = 1;

    public EyeSocketWrap(SourceIdent ident, Handler handler, EyeBuffer rawBuffer,
            EyeSocket socket,int Version) {
        mIdent = ident;
        mTransHandler = handler;
        mRawBuffer = rawBuffer;
        mSocket = socket;
        mSocketBuffer = null;
        this.Version = Version;
    }

    public SourceIdent getIdent() {
        return mIdent;
    }

    public Handler getHandler() {
        return mTransHandler;
    }

    public EyeSocket getSocket() {
        return mSocket;
    }
    
    public boolean isSameIdent(SourceIdent ident) {
        boolean result = mIdent.isSameIdent(ident);
        return result;
    }
    
    public byte[] getSocketBuffer(int size) {
        if (null != mSocketBuffer && mSocketBuffer.length >= size) {
            return mSocketBuffer;
        }
        
        byte[] result = null;
        try {
            mSocketBuffer = new byte[size];
            result = mSocketBuffer;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public void clear() {
        if (null != mSocket)
            mSocket.close();
        
        mRawBuffer = null;
        mSocketBuffer = null;
        if (null != mRawDataStack) {
            mRawDataStack.clear();
        }
    }

    public boolean writeRawData(byte[] buffer, int offset, int size) {
        if (null == mRawBuffer)
            return false;
        
        boolean needStack = false;
        byte[] rawData = null;
        int rawDataLen = 0;
        int count = 0;
        
        if (null != mRawDataStack)
            count = mRawDataStack.size();
        
        for (int i = 0; i < count; i++) {
            rawData = mRawDataStack.getFirst();
            rawDataLen = rawData.length;
            mRawBuffer.lock();
            boolean suc = mRawBuffer.write(rawData, 0, rawDataLen);
            mRawBuffer.unlock();
            
            if (!suc) {
                needStack = true;
                mRawBuffer.setNeedCompact(true);
                break;
            }
            
            mRawDataStack.removeFirst();
        }
        
        if (!needStack) {
            mRawBuffer.lock();
            boolean suc = mRawBuffer.write(buffer, offset, size);
            mRawBuffer.unlock();
            
            if (suc)
                return true;
            else {
                mRawBuffer.setNeedCompact(true);
            }
        }
        
        try {
            if (null == mRawDataStack)
                mRawDataStack = new LinkedList<byte[]>();
                
            rawData = new byte[size];
            System.arraycopy(buffer, offset, rawData, 0, size);
            mRawDataStack.add(rawData);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        
        return true;
    }
}